﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FATCATool
{
    public partial class CorrectableOganizationControl : UserControl
    {
        private CorrectableOrganisationParty_Type _correctableParty;
        private OrganisationParty_Type _party;
        private bool _bindingComplete = false;

        public CorrectableOganizationControl()
        {
            InitializeComponent();
        }

        public void Intitalize(CorrectableOrganisationParty_Type party)
        {
            _bindingComplete = false;
            _correctableParty = party;
            DataBind();
            grpDocSpec.Visible = true;
            docSpecControl.Intitalize(_correctableParty.DocSpec);
            addressControl.Initialize(_correctableParty);
            _bindingComplete = true;
        }

        public void Intitalize(OrganisationParty_Type party)
        {
            _bindingComplete = false;
            _party = party;
            DataBind();
            grpDocSpec.Visible = false;
            addressControl.Initialize(_party);
            _bindingComplete = true;
        }

        private void DataBind()
        {
            ((DataGridViewComboBoxColumn)grdNames.Columns["nameType"]).DataSource = null;
            ((DataGridViewComboBoxColumn)grdNames.Columns["nameType"]).DataSource = FATCA_OECD.GetFriendlyEnumList(typeof(OECDNameType_EnumType));
            ((DataGridViewComboBoxColumn)grdNames.Columns["nameType"]).ValueMember = "Key";
            ((DataGridViewComboBoxColumn)grdNames.Columns["nameType"]).DisplayMember = "Value";

            ((DataGridViewComboBoxColumn)grdCountry.Columns["nameDataGridViewTextBoxColumn"]).DataSource = Enum.GetValues(typeof(CountryCode_Type));
            ((DataGridViewComboBoxColumn)grdTIN.Columns["issuedByDataGridViewTextBoxColumn"]).DataSource = Enum.GetValues(typeof(CountryCode_Type));

            if (_correctableParty != null)
            {
                nameOrganisationTypeBindingSource.DataSource = _correctableParty.Name.ToList();

                if (_correctableParty.ResCountryCode != null)
                    genericNameBindingSource.DataSource = _correctableParty.ResCountryCode.ToList().Select(r => new FATCASchema.Utilities.GenericCountry { Name = r }).ToList();

                if (_correctableParty.TIN != null)
                    tINTypeBindingSource.DataSource = _correctableParty.TIN.ToList();
                else
                    tINTypeBindingSource.Clear();
            }
            else
            {
                nameOrganisationTypeBindingSource.DataSource = _party.Name.ToList();

                if (_party.ResCountryCode != null)
                    genericNameBindingSource.DataSource = _party.ResCountryCode.ToList().Select(r => new FATCASchema.Utilities.GenericCountry { Name = r }).ToList();

                if (_party.TIN != null)
                    tINTypeBindingSource.DataSource = _party.TIN.ToList();
                else
                    tINTypeBindingSource.Clear();
            }

        }


        private void grdNames_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            if (!_bindingComplete || (_party == null && _correctableParty == null) || nameOrganisationTypeBindingSource.DataSource == null)
                return;

            if (nameOrganisationTypeBindingSource.DataSource is List<NameOrganisation_Type>)
                if (_correctableParty!= null)
                    _correctableParty.Name = ((List<NameOrganisation_Type>)nameOrganisationTypeBindingSource.DataSource).ToArray();
                else
                    _party.Name = ((List<NameOrganisation_Type>)nameOrganisationTypeBindingSource.DataSource).ToArray();
        }

        private void grdNames_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!_bindingComplete || (_party == null && _correctableParty == null) || nameOrganisationTypeBindingSource.DataSource == null)
                return;

            if (nameOrganisationTypeBindingSource.DataSource is List<NameOrganisation_Type>)
                if (_correctableParty != null)
                    _correctableParty.Name = ((List<NameOrganisation_Type>)nameOrganisationTypeBindingSource.DataSource).ToArray();
                else
                    _party.Name = ((List<NameOrganisation_Type>)nameOrganisationTypeBindingSource.DataSource).ToArray();
        }

        private void grdCountry_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            if (!_bindingComplete || (_party == null && _correctableParty == null) || genericNameBindingSource.DataSource == null)
                return;

            if (genericNameBindingSource.DataSource is List<FATCASchema.Utilities.GenericCountry>)
                if (_correctableParty != null)
                    _correctableParty.ResCountryCode = ((List<FATCASchema.Utilities.GenericCountry>)genericNameBindingSource.DataSource).Select(r => r.Name).ToArray();
                else
                    _party.ResCountryCode = ((List<FATCASchema.Utilities.GenericCountry>)genericNameBindingSource.DataSource).Select(r => r.Name).ToArray();
        }

        private void grdCountry_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!_bindingComplete || (_party == null && _correctableParty == null) || genericNameBindingSource.DataSource == null)
                return;

            if (genericNameBindingSource.DataSource is List<FATCASchema.Utilities.GenericCountry>)
                if (_correctableParty != null)
                    _correctableParty.ResCountryCode = ((List<FATCASchema.Utilities.GenericCountry>)genericNameBindingSource.DataSource).Select(r => r.Name).ToArray();
                else
                    _party.ResCountryCode = ((List<FATCASchema.Utilities.GenericCountry>)genericNameBindingSource.DataSource).Select(r => r.Name).ToArray();
        }

        private void grdTIN_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            if (!_bindingComplete || (_party == null && _correctableParty == null) || tINTypeBindingSource.DataSource == null)
                return;

            if (tINTypeBindingSource.DataSource is List<TIN_Type>)
                if (_correctableParty != null)
                    _correctableParty.TIN = ((List<TIN_Type>)tINTypeBindingSource.DataSource).ToArray();
                else
                    _party.TIN = ((List<TIN_Type>)tINTypeBindingSource.DataSource).ToArray();
        }

        private void grdTIN_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!_bindingComplete || (_party == null && _correctableParty == null) || tINTypeBindingSource.DataSource == null)
                return;

            if (tINTypeBindingSource.DataSource is List<TIN_Type>)
                if (_correctableParty != null)
                    _correctableParty.TIN = ((List<TIN_Type>)tINTypeBindingSource.DataSource).ToArray();
                else
                    _party.TIN = ((List<TIN_Type>)tINTypeBindingSource.DataSource).ToArray();
        }


    }
}
